package com.lnyt.dao.t12306;

import com.alibaba.fastjson.JSONObject;
import com.lnyt.pojo.ticket.TicketTask;
import com.lnyt.pojo.ticket.TicketUser;
import com.lnyt.util.factroy.TicketFactroy;
import okhttp3.OkHttpClient;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Component
public class TicketStore {
    // 已登录
    public static final int LOGINED = 0;
    // 未登录
    public static final int NOLOGIN = 1;
    // 有未完成订单
    public static final int NOCOMPLETE = 2;

    // 用户表
    private volatile Map<String, TicketUser> ticketUserMap;

    // 初始化
    @PostConstruct
    public void initStore() {
        ticketUserMap = new HashMap<>();
    }

    // user对象表
    public Map<String, TicketUser> getTicketUserMap() {
        return ticketUserMap;
    }

    public void setTicketUserMap(Map<String, TicketUser> ticketUserMap) {
        this.ticketUserMap = ticketUserMap;
    }

    // Http对象
    public OkHttpClient getUserOkHttpClient(String userId) {
        return ticketUserMap.get(userId).getOkHttpClient();
    }


    public void setUserOkHttpClient(String userId, OkHttpClient okHttpClient) {
        if (!ticketUserMap.containsKey(userId)) {
            ticketUserMap.put(userId, TicketFactroy.creatTicketUser());
        }

        ticketUserMap.get(userId).setOkHttpClient(okHttpClient);
    }

    public List<OkHttpClient> getUserOkHttpClientList() {
        List<OkHttpClient> result = new ArrayList<>();

        for (TicketUser ticketUser : ticketUserMap.values()) {
            if (ticketUser.getTicketStatus() != TicketStore.LOGINED) {
                continue;
            }

            result.add(ticketUser.getOkHttpClient());
        }

        return result;
    }

    // Json 数据表
    public List<JSONObject> getUserTaskReqList() {
        Map<String, JSONObject> result = new HashMap<>();

        for (TicketUser ticketUser: ticketUserMap.values()) {
            if (ticketUser.getTicketStatus() != LOGINED) {
                continue;
            }

            for (TicketTask ticketTask : ticketUser.getTicketTaskMap().values()) {
                String status = ticketTask.getStatus();
                String trainKey = ticketTask.getTrainId();

                if (status == null || !status.equals("Work")) {
                    continue;
                }

                if (!result.containsKey(trainKey)) {
                    JSONObject reqData = new JSONObject();
                    reqData.put("userKey",     ticketTask.getUserId());
                    reqData.put("trainKey",    ticketTask.getTrainId());
                    reqData.put("train_date",  ticketTask.getTrainDate());
                    reqData.put("fromStation", ticketTask.getFromStation());
                    reqData.put("toStation",   ticketTask.getToStation());
                    reqData.put("trainNumber", ticketTask.getTrainNumber());

                    result.put(trainKey, reqData);
                }
            }
        }

        return new ArrayList<>(result.values());
    }

    // 任务
    public void insetUserTask(String userId, TicketTask ticketTask) {
        if (!ticketUserMap.containsKey(userId)) {
            ticketUserMap.put(userId, TicketFactroy.creatTicketUser());
        }

        Integer taskId = ticketTask.getId();
        ticketUserMap.get(userId).getTicketTaskMap().put(taskId, ticketTask);
    }

    public void deleteUserTask(String userId, TicketTask ticketTask) {
        Integer taskId = ticketTask.getId();
        ticketUserMap.get(userId).getTicketTaskMap().remove(taskId);

        if (ticketUserMap.get(userId).getTicketTaskMap().isEmpty()) {
            ticketUserMap.remove(userId);
        }
    }

    public void updataUserTask(String userId, TicketTask ticketTask) {
        Integer taskId = ticketTask.getId();

        ticketUserMap.get(userId).getTicketTaskMap().put(taskId, ticketTask);
    }

    public TicketTask selectUserTask(String userId, TicketTask ticketTask) {
        Integer taskId = ticketTask.getId();

        return ticketUserMap.get(userId).getTicketTaskMap().get(taskId);
    }
}
